/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Services;

import BackEnd.Categoria;
import BackEnd.Linker;
import BackEnd.Producto;
import BackEnd.SubCategoria;
import java.util.Map;
import java.util.TreeMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


/**
 *
 * @author Gabi
 */
public class Catalog {

    private static final String urlS="http://eiffel.itba.edu.ar/hci/service/Catalog.groovy";
	
    public static Map<Integer,Categoria> getCategoryList(int language_id) throws Exception {
        int i;
         
        Map<Integer,Categoria> cats = new TreeMap<Integer,Categoria>();
         
        String[] args = {"language_id="+language_id};
         
        Document doc = Linker.getData(urlS,"GetCategoryList","GET",args);
         
        NodeList nodes = doc.getElementsByTagName("category");
         
        for (i = 0; i < nodes.getLength(); i++) {
            Element e = (Element) nodes.item(i);
            int id = new Integer(e.getAttribute("id"));
            String name= ((e.getElementsByTagName("name")).item(0)).getTextContent();
            String code= ((e.getElementsByTagName("code")).item(0)).getTextContent();
            Categoria cat= new Categoria(id,name,code);
            cats.put(id,cat);
        }
         
        return cats;
    }

    public static Map<Integer,SubCategoria> getSubCategoryList(int language_id, int category_id) throws Exception {
         int i;
         
         Map<Integer,SubCategoria> subCats=new TreeMap<Integer,SubCategoria>();
         
         String[] args = {"language_id="+language_id,"category_id="+category_id};
         
         Document doc = Linker.getData(urlS,"GetSubcategoryList","GET",args);
         
         NodeList nodes = doc.getElementsByTagName("subcategory");
         
         for (i = 0; i < nodes.getLength(); i++) {
        	 Element e = (Element) nodes.item(i);
             int id = new Integer(e.getAttribute("id"));
             int catId = Integer.parseInt(((e.getElementsByTagName("category_id")).item(0)).getTextContent());
             String name= ((e.getElementsByTagName("name")).item(0)).getTextContent();
             String code= ((e.getElementsByTagName("code")).item(0)).getTextContent();
             SubCategoria subCat= new SubCategoria(id,catId,name,code);
             subCats.put(id,subCat);
         }
         
         return subCats;
    }

    public static Map<Integer,Producto> getProductListByCategory(int language_id, int category_id) throws Exception {
        int i;

        Map<Integer,Producto> prods = new TreeMap<Integer,Producto>();

        String[] args = {"language_id="+language_id,"category_id="+category_id};

        Document doc = Linker.getData(urlS,"GetProductListByCategory","GET",args);

        NodeList nodes = doc.getElementsByTagName("product");

        for (i = 0; i < nodes.getLength(); i++) {
            Element e = (Element) nodes.item(i);
            int id = new Integer(e.getAttribute("id"));
            String name= ((e.getElementsByTagName("name")).item(0)).getTextContent();
            int cat_id = (new Integer(((e.getElementsByTagName("category_id")).item(0)).getTextContent())).intValue();
            int sub_id = (new Integer(((e.getElementsByTagName("subcategory_id")).item(0)).getTextContent())).intValue();
            double rank= (new Double(((e.getElementsByTagName("sales_rank")).item(0)).getTextContent())).doubleValue();
            double price=(new Double(((e.getElementsByTagName("price")).item(0)).getTextContent())).doubleValue();
            String image_url=((e.getElementsByTagName("image_url")).item(0)).getTextContent();
            Producto prod= new Producto(id,cat_id,sub_id,name,rank,price,image_url);
            prods.put(id,prod);
        }

        return prods;
    }

    public static Map<Integer,Producto> getProductListBySubCategory(int language_id, int category_id, int subcategory_id) throws Exception {
        int i;

        Map<Integer,Producto> prods = new TreeMap<Integer,Producto>();

        String[] args = {"language_id="+language_id,"category_id="+category_id,"subcategory_id="+subcategory_id};

        Document doc = Linker.getData(urlS,"GetProductListBySubcategory","GET",args);

        NodeList nodes = doc.getElementsByTagName("product");

        for (i = 0; i < nodes.getLength(); i++) {
            Element e = (Element) nodes.item(i);
            int id = new Integer(e.getAttribute("id"));
            String name= ((e.getElementsByTagName("name")).item(0)).getTextContent();
            int cat_id = (new Integer(((e.getElementsByTagName("category_id")).item(0)).getTextContent())).intValue();
            int sub_id = (new Integer(((e.getElementsByTagName("subcategory_id")).item(0)).getTextContent())).intValue();
            double rank= (new Double(((e.getElementsByTagName("sales_rank")).item(0)).getTextContent())).doubleValue();
            double price=(new Double(((e.getElementsByTagName("price")).item(0)).getTextContent())).doubleValue();
            String image_url=((e.getElementsByTagName("image_url")).item(0)).getTextContent();
            Producto prod= new Producto(id,cat_id,sub_id,name,rank,price,image_url);
            prods.put(id,prod);
        }

        return prods;
    }

    public static Map<Integer,Producto> getProductListByName(String criterio) throws Exception {
        int i;

        Map<Integer,Producto> prods = new TreeMap<Integer,Producto>();

        String[] args = {"criteria="+criterio};

        Document doc = Linker.getData(urlS,"GetProductListByName","GET",args);

        NodeList nodes = doc.getElementsByTagName("product");

        for (i = 0; i < nodes.getLength(); i++) {
            Element e = (Element) nodes.item(i);
            int id = new Integer(e.getAttribute("id"));
            String name= ((e.getElementsByTagName("name")).item(0)).getTextContent();
            int cat_id = (new Integer(((e.getElementsByTagName("category_id")).item(0)).getTextContent())).intValue();
            int sub_id = (new Integer(((e.getElementsByTagName("subcategory_id")).item(0)).getTextContent())).intValue();
            double rank= (new Double(((e.getElementsByTagName("sales_rank")).item(0)).getTextContent())).doubleValue();
            double price=(new Double(((e.getElementsByTagName("price")).item(0)).getTextContent())).doubleValue();
            String image_url=((e.getElementsByTagName("image_url")).item(0)).getTextContent();
            Producto prod= new Producto(id,cat_id,sub_id,name,rank,price,image_url);
            prods.put(id,prod);
        }

        return prods;
    }

    public static Producto getProduct(Integer product_id) throws Exception {

        String[] args = {"product_id="+product_id};

        Document doc = Linker.getData(urlS,"GetProduct","GET",args);

        NodeList nodes = doc.getElementsByTagName("product");

        Element e = (Element) nodes.item(0);
        int id = new Integer(e.getAttribute("id"));
        String name= ((e.getElementsByTagName("name")).item(0)).getTextContent();
        int cat_id = (new Integer(((e.getElementsByTagName("category_id")).item(0)).getTextContent())).intValue();
        int sub_id = (new Integer(((e.getElementsByTagName("subcategory_id")).item(0)).getTextContent())).intValue();
        double rank= (new Double(((e.getElementsByTagName("sales_rank")).item(0)).getTextContent())).doubleValue();
        double price=(new Double(((e.getElementsByTagName("price")).item(0)).getTextContent())).doubleValue();
        String image_url=((e.getElementsByTagName("image_url")).item(0)).getTextContent();
        try{
        String actors=((e.getElementsByTagName("actors")).item(0)).getTextContent();
        String format=((e.getElementsByTagName("format")).item(0)).getTextContent();
        String language=((e.getElementsByTagName("language")).item(0)).getTextContent();
        String subtitles=((e.getElementsByTagName("subtitles")).item(0)).getTextContent();
        String region=((e.getElementsByTagName("region")).item(0)).getTextContent();
        String aspect_ratio=((e.getElementsByTagName("aspect_ratio")).item(0)).getTextContent();
        int number_discs = (new Integer(((e.getElementsByTagName("number_discs")).item(0)).getTextContent())).intValue();
        String release_date=((e.getElementsByTagName("release_date")).item(0)).getTextContent();
        int run_time = (new Integer(((e.getElementsByTagName("run_time")).item(0)).getTextContent())).intValue();
        String ASIN=((e.getElementsByTagName("ASIN")).item(0)).getTextContent();
        
        Producto prod= new Producto(id,cat_id,sub_id,name,rank,price,image_url,
                actors,format,language,subtitles,region,aspect_ratio,number_discs,
                release_date,run_time,ASIN);

        return prod;
        }catch(NullPointerException e2){

            String language=((e.getElementsByTagName("language")).item(0)).getTextContent();
            String authors=((e.getElementsByTagName("authors")).item(0)).getTextContent();
            String publisher=((e.getElementsByTagName("publisher")).item(0)).getTextContent();
            String published_date=((e.getElementsByTagName("published_date")).item(0)).getTextContent();
            String ISBN_10=((e.getElementsByTagName("ISBN_10")).item(0)).getTextContent();
            String ISBN_13=((e.getElementsByTagName("ISBN_13")).item(0)).getTextContent();

            Producto prod= new Producto(id,cat_id,sub_id,name,rank,price,image_url,
                language,authors,publisher,published_date,ISBN_10,ISBN_13);

            return prod;
        }
    }

}
